﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using eAssessment.questions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Linq;
using eAssessment.SPLinqPortal;

namespace eAssessment.lists
{
    class MChoiceQuestionTemplateListManager
    {
        

        public MultipleChoiceQuestionTemplate GetMultipleChoiceQuestionTemplate(string id, SPWeb listWeb)
        {
            DataContext data = new DataContext(listWeb.Url);
            EntityList<QuestionTemplate> questions = data.GetList<QuestionTemplate>(ListAccessManager.QuestionTemplateListName);
            var queryQuestion = from quest in questions
                                where quest.ID == int.Parse(id)
                                select quest;
            QuestionTemplate question = queryQuestion.First();

            MultipleChoiceQuestionTemplate template = null;
            if (question != null)
            {
                DataContext data2 = new DataContext(listWeb.Url);
                EntityList<MChoiceAnswerTemplate> answers = data.GetList<MChoiceAnswerTemplate>(ListAccessManager.MChoiceAnswerTemplateListName);
                var queryAnswers = from ans in answers
                                   where ans.QuestionTemplateID == question.ID
                                   select ans;

                template = new MultipleChoiceQuestionTemplate(question);
                foreach (var ans in queryAnswers)
                {
                    template.AddAnswer(ans.AnswerText, bool.Parse(ans.IsCorrectAnswer.ToString()));
                }
            }
            return template;
        }
    }
}
